package com.ikkong.contract.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.projects.model.Projects;
import com.ikkong.projects.service.ProjectsService;
import com.ikkong.projects.service.impl.ProjectsServiceImpl;
import com.ikkong.suppliers.model.Suppliers;
import com.ikkong.suppliers.service.SuppliersService;
import com.ikkong.suppliers.service.impl.SuppliersServiceImpl;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.contract.model.Contract;
import com.ikkong.contract.service.ContractService;
import com.ikkong.contract.service.impl.ContractServiceImpl;
import com.ikkong.system.model.Dept;
import com.jfinal.upload.UploadFile;

/**
 * Generated by Blade.
 * 2018-03-29 16:26:45
 */
public class ContractController extends UrlPermissController {
	private static String CODE = "contract";
	private static String PERFIX = "contract";
	private static String LIST_SOURCE = "Contract.list";
	private static String BASE_PATH = "/platform/contract/";
	private static String PROJECTS_PATH = "/platform/projects/";

	ContractService service = new ContractServiceImpl();
	ProjectsService ps = new ProjectsServiceImpl();
	SuppliersService ss = new SuppliersServiceImpl();

	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "contract.html");
	}
    public void annex() {
        String id = getPara("id");
        Contract c = service.findById(id);
        if(c!=null){
            if(c.getAnnex()!=null){
                String[] files = c.getAnnex().split(",");
                List<String> f = (List<String>) Arrays.asList(files);
                setAttr("files",f);
            }

        }
        setAttr("code", CODE);
        render(BASE_PATH + "annex.html");
    }
    public void annexfile() {
        String file = getPara("file");
        renderFile(file);
    }
    public void supp() {
        renderText("程序员还在开发此功能");
    }

	public void add() {

		List<Suppliers> suppliersList = ss.findAll();
		Integer id = getParaToInt("id");
        String menu = getPara("menu");
		Projects pro = ps.findById(id);
		Integer did = pro.getDept_id();

		Dept dept = Blade.create(Dept.class).findById(did);
        setAttr("supp",suppliersList);
		setAttr("dept",dept);
		setAttr("pro",pro);
		setAttr("code", CODE);
		setAttr("menu", menu);
		render(BASE_PATH + "contract_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Contract contract = service.findById(id);
		setAttr("model", JsonKit.toJson(contract));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "contract_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Contract contract = service.findById(id);
		setAttr("model", JsonKit.toJson(contract));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "contract_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
        UploadFile file = getFile("annex");
        String filename = "";
        if(file != null){
            filename = file.getFileName();
        }
        String[] supps = getParaValues("select");
        String menu = getPara("menu");
        String sup = "";
        for (int i = 0; i < supps.length; i++) {
            sup = sup + supps[i] + ",";
        }
        Contract contract = mapping(PERFIX, Contract.class);
        Integer pro_id = contract.getPro_id();
        Projects byId = Blade.create(Projects.class).findById(pro_id);
        byId.setStatus(16);
        Blade.create(Projects.class).update(byId);
        contract.setSuppliers_ids(sup);
        contract.setAnnex(filename);
		boolean temp = service.save(contract);
		if (temp) {
		        setAttr("href","/contract/");
            setAttr("msg","跳转到合同列表");
                render(BASE_PATH + "../projects/success.html");
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Contract contract = mapping(PERFIX, Contract.class);
		contract.setVersion(contract.getVersion()+1);
		boolean temp = service.update(contract);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
